package ${config.basePackage}.provider;

import java.text.MessageFormat;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

import ${config.basePackage}.entity.${tableinfo.tableName?cap_first};
import ${config.basePackage}.entity.query.${tableinfo.tableName?cap_first}Query;
import ${config.basePackage}.query.QueryColumn;
import ${config.basePackage}.query.QueryExpreEnum;
import ${config.basePackage}.query.QueryOrder;
import ${config.basePackage}.query.QueryPageInfo;

/**
 * <#if tableinfo.tableComment ?? > ${tableinfo.tableComment}  </#if>
 *
 * ${.now?string('yyyy-MM-dd HH:mm:ss')}
 * 
 * @author 工具自动生成       Lv Bu
 *
 */
public class ${tableinfo.tableName?cap_first}SqlProvider {

	/**
	 * 插入单条数据
	 * 
	 * @return
	 */
	public String insert${tableinfo.tableName?cap_first}(@Param("${tableinfo.tableName}") ${tableinfo.tableName?cap_first} ${tableinfo.tableName}) {
		return new SQL() {
			{
				INSERT_INTO("${tableinfo.dbTableName}");
				INTO_COLUMNS(<#list tableinfo.columns as column><#if tableinfo.idField==column.columnName && column.columnType!='Integer'>"${column.dbColumnName}",</#if><#if tableinfo.idField!=column.columnName>"${column.dbColumnName}"<#if (column_index+1)<tableinfo.columns?size>,</#if></#if></#list>);
				INTO_VALUES(<#list tableinfo.columns as column><#if tableinfo.idField==column.columnName && column.columnType!='Integer'>"${r'#{'}${tableinfo.tableName}.${column.columnName}${r'}'}",</#if><#if tableinfo.idField!=column.columnName>"${r'#{'}${tableinfo.tableName}.${column.columnName}${r'}'}"<#if (column_index+1)<tableinfo.columns?size>,</#if></#if></#list>);
			}
		}.toString();
	};


	<#if tableinfo.idField!=''>
	// 更新
	public String update${tableinfo.tableName?cap_first}ByPK(@Param("${tableinfo.tableName}") ${tableinfo.tableName?cap_first} ${tableinfo.tableName}) {
		return new SQL() {
			{
				UPDATE("${tableinfo.dbTableName}");
				<#list tableinfo.columns as column>
				<#if tableinfo.idField!=column.columnName>
				SET("${column.dbColumnName}=${r'#{'}${tableinfo.tableName}.${column.columnName}${r'}'}");
				</#if>
				</#list>
				WHERE("${tableinfo.dbIdField}=${r'#{'}${tableinfo.tableName}.${tableinfo.idField}${r'}'}");
			}
		}.toString();
	}

	// 更新不为null的属性值
	public String update${tableinfo.tableName?cap_first}NotNullByPK(@Param("${tableinfo.tableName}") ${tableinfo.tableName?cap_first} ${tableinfo.tableName}) {
		return new SQL() {
			{
				UPDATE("${tableinfo.dbTableName}");
				<#list tableinfo.columns as column>
				<#if tableinfo.idField!=column.columnName>
				if (${tableinfo.tableName}.get${column.columnName?cap_first}() != null){
					SET("${column.dbColumnName}=${r'#{'}${tableinfo.tableName}.${column.columnName}${r'}'}");
				}
				</#if>
				</#list>
				WHERE("${tableinfo.dbIdField}=${r'#{'}${tableinfo.tableName}.${tableinfo.idField}${r'}'}");
			}
		}.toString();
	}

	// 更新不为null的属性值
	public String update${tableinfo.tableName?cap_first}NotEmptyByPK(@Param("${tableinfo.tableName}") ${tableinfo.tableName?cap_first} ${tableinfo.tableName}) {
		return new SQL() {
			{
				UPDATE("${tableinfo.dbTableName}");
				<#list tableinfo.columns as column>
				<#if tableinfo.idField!=column.columnName>
				<#if column.columnType=='String'>
				if (${tableinfo.tableName}.get${column.columnName?cap_first}() != null && !${tableinfo.tableName}.get${column.columnName?cap_first}().trim().equals("")){
				<#else>
				if (${tableinfo.tableName}.get${column.columnName?cap_first}() != null){
				</#if>
					SET("${column.dbColumnName}=${r'#{'}${tableinfo.tableName}.${column.columnName}${r'}'}");
				}else{
					SET("${column.dbColumnName}=${r'#{'}${tableinfo.tableName}.${column.columnName}${r'}'}");
				}
				</#if>
				</#list>
				WHERE("${tableinfo.dbIdField}=${r'#{'}${tableinfo.tableName}.${tableinfo.idField}${r'}'}");
			}
		}.toString();
	}

	// 更新不为""并且不为null的属性值
	public String update${tableinfo.tableName?cap_first}NotBlankByPK(@Param("${tableinfo.tableName}") ${tableinfo.tableName?cap_first} ${tableinfo.tableName}) {
		return new SQL() {
			{
				UPDATE("${tableinfo.dbTableName}");
				<#list tableinfo.columns as column>
				<#if tableinfo.idField!=column.columnName>
				<#if column.columnType=='String'>
				if (${tableinfo.tableName}.get${column.columnName?cap_first}() != null && !${tableinfo.tableName}.get${column.columnName?cap_first}().trim().equals("")){
				<#else>
				if (${tableinfo.tableName}.get${column.columnName?cap_first}() != null){
				</#if>
					SET("${column.dbColumnName}=${r'#{'}${tableinfo.tableName}.${column.columnName}${r'}'}");
				}
				</#if>
				</#list>
				WHERE("${tableinfo.dbIdField}=${r'#{'}${tableinfo.tableName}.${tableinfo.idField}${r'}'}");
			}
		}.toString();
	}

	// 根据主键删
	public String delete${tableinfo.tableName?cap_first}ByPk(@Param("id") <#list tableinfo.columns as column><#if tableinfo.idField==column.columnName>${column.columnType}<#break></#if></#list> id) {
		return new SQL() {
			{
				DELETE_FROM("${tableinfo.dbTableName}");
				WHERE("${tableinfo.dbIdField}=${r'#{id}'}");
			}
		}.toString();
	}

	// 根据ID查找数据
	public String select${tableinfo.tableName?cap_first}ById(@Param("id") <#list tableinfo.columns as column><#if tableinfo.idField==column.columnName>${column.columnType}<#break></#if></#list> id) {
		return new SQL() {
			{
				SELECT("*");
				FROM("${tableinfo.dbTableName}");
				WHERE("${tableinfo.dbIdField}=${r'#{id}'}");
			}
		}.toString();
	}

	// 根据主键查找并指定显示的列
	public String select${tableinfo.tableName?cap_first}CusColumnById(@Param("columns") String[] columns, @Param("id") <#list tableinfo.columns as column><#if tableinfo.idField==column.columnName>${column.columnType}<#break></#if></#list> id) {
		return new SQL() {
			{
				if (columns == null || columns.length == 0) {
					SELECT("*");
				} else {
					SELECT(columns);
				}
				FROM("${tableinfo.dbTableName}");
				WHERE("${tableinfo.dbIdField}=${r'#{id}'}");
			}
		}.toString();
	}
	</#if>

	// 批量插入
	public String insertList${tableinfo.tableName?cap_first}(@Param("list") List<${tableinfo.tableName?cap_first}> ${tableinfo.tableName}s) {
		StringBuffer sbf = new StringBuffer(
				"INSERT INTO ${tableinfo.dbTableName} (<#list tableinfo.columns as column><#if tableinfo.idField==column.columnName && column.columnType!='Integer'>${column.dbColumnName},</#if><#if tableinfo.idField!=column.columnName>${column.dbColumnName}<#if (column_index+1)<tableinfo.columns?size>,</#if></#if></#list>) VALUES ");
		MessageFormat mf = new MessageFormat(
				"(<#list tableinfo.columns as column><#if tableinfo.idField==column.columnName && column.columnType!='Integer'><#noparse>#'{'list[{0}].</#noparse>${column.columnName}<#noparse>'}'</#noparse>,</#if><#if tableinfo.idField!=column.columnName><#noparse>#'{'list[{0}].</#noparse>${column.columnName}<#noparse>'}'</#noparse><#if (column_index+1)<tableinfo.columns?size>,</#if></#if></#list>)");
		for (int i = 0; i < ${tableinfo.tableName}s.size(); i++) {
			sbf.append(mf.format(new Object[] { String.valueOf(i) }));
			if (i < ${tableinfo.tableName}s.size() - 1) {
				sbf.append(",");
			}
		}
		return sbf.toString();
	}

	// 根据条件更新
	public String update${tableinfo.tableName?cap_first}ByQuery(@Param("query")${tableinfo.tableName?cap_first}Query query,@Param("${tableinfo.tableName}")${tableinfo.tableName?cap_first} ${tableinfo.tableName}) {
		
		return new SQL() {
			{
				
				UPDATE("${tableinfo.dbTableName}");
				<#list tableinfo.columns as column>
				<#if tableinfo.idField!=column.columnName>
				<#if column.columnType=='String'>
				if (${tableinfo.tableName}.get${column.columnName?cap_first}() != null && !${tableinfo.tableName}.get${column.columnName?cap_first}().trim().equals("")){
				<#else>
				if (${tableinfo.tableName}.get${column.columnName?cap_first}() != null){
				</#if>
					SET("${column.dbColumnName}=${r'#{'}${tableinfo.tableName}.${column.columnName}${r'}'}");
				}
				</#if>
				</#list>
				if (query != null) {
					List<QueryColumn> querys = query.getQueryColumns();
					if (querys != null && querys.size() > 0) {
						MessageFormat mf = null;
						for (int i = 0; i < querys.size(); i++) {
							QueryColumn qc = querys.get(i);
							if (i != 0) {
								if (qc.getMosaic().equals("AND")) {
									AND();
								} else {
									OR();
								}
							}
							if (qc.getFiledName() != null) {
								if (!qc.getFiledExpre().equals(QueryExpreEnum.ISNOTNULL)
										&& !qc.getFiledExpre().equals(QueryExpreEnum.ISNULL)
										&& !qc.getFiledExpre().equals(QueryExpreEnum.IN)) {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre()
											+ " #'{'query.queryColumns[{0}].value'}'");
								} else if (qc.getFiledExpre().equals(QueryExpreEnum.IN)) {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre()
											+ " (" + qc.getValue() + ")");
								} else {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre());
								}
								WHERE(mf.format(new Object[] { String.valueOf(i) }));
							}
							if (qc.getChildQueryColumns() != null && qc.getChildQueryColumns().size() > 0) {
								if (qc.getMosaic().equals("AND")) {
									AND();
								} else {
									OR();
								}
								List<QueryColumn> childQueryColumns = qc.getChildQueryColumns();
								StringBuffer sbf = new StringBuffer("(");
								for (int j = 0; j < childQueryColumns.size(); j++) {
									QueryColumn cqc = childQueryColumns.get(j);
									if (cqc.getFiledName() != null) {
										if (j != 0 && j < childQueryColumns.size() - 1) {
											sbf.append(cqc.getMosaic());
										}
										if (!cqc.getFiledExpre().equals(QueryExpreEnum.ISNOTNULL)
												&& !cqc.getFiledExpre().equals(QueryExpreEnum.ISNULL)
												&& !cqc.getFiledExpre().equals(QueryExpreEnum.IN)) {
											mf = new MessageFormat(cqc.getFiledName() + " "
													+ cqc.getFiledExpre().getExpre()
													+ " #'{'query.queryColumns[{0}].childQueryColumns[{1}].value'}' ");
										} else if (cqc.getFiledExpre().equals(QueryExpreEnum.IN)) {
											mf = new MessageFormat(cqc.getFiledName() + " "
													+ cqc.getFiledExpre().getExpre() + " (" + cqc.getValue() + ")");
										} else {
											mf = new MessageFormat(
													cqc.getFiledName() + " " + cqc.getFiledExpre().getExpre() + " ");
										}
										sbf.append(mf.format(new Object[] { i, j }));
									}
								}
								sbf.append(")");
								WHERE(sbf.toString());
							}
						}
					}
				}
			}
		}.toString();
	}

	// 根据条件删除
	public String delete${tableinfo.tableName?cap_first}ByQuery(@Param("query")${tableinfo.tableName?cap_first}Query query) {
		
		return new SQL() {
			{
				DELETE_FROM("${tableinfo.dbTableName}");
				if (query != null) {
					List<QueryColumn> querys = query.getQueryColumns();
					if (querys != null && querys.size() > 0) {
						MessageFormat mf = null;
						for (int i = 0; i < querys.size(); i++) {
							QueryColumn qc = querys.get(i);
							if (i != 0) {
								if (qc.getMosaic().equals("AND")) {
									AND();
								} else {
									OR();
								}
							}
							if (qc.getFiledName() != null) {
								if (!qc.getFiledExpre().equals(QueryExpreEnum.ISNOTNULL)
										&& !qc.getFiledExpre().equals(QueryExpreEnum.ISNULL)
										&& !qc.getFiledExpre().equals(QueryExpreEnum.IN)) {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre()
											+ " #'{'query.queryColumns[{0}].value'}'");
								} else if (qc.getFiledExpre().equals(QueryExpreEnum.IN)) {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre()
											+ " (" + qc.getValue() + ")");
								} else {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre());
								}
								WHERE(mf.format(new Object[] { String.valueOf(i) }));
							}
							if (qc.getChildQueryColumns() != null && qc.getChildQueryColumns().size() > 0) {
								if (qc.getMosaic().equals("AND")) {
									AND();
								} else {
									OR();
								}
								List<QueryColumn> childQueryColumns = qc.getChildQueryColumns();
								StringBuffer sbf = new StringBuffer("(");
								for (int j = 0; j < childQueryColumns.size(); j++) {
									QueryColumn cqc = childQueryColumns.get(j);
									if (cqc.getFiledName() != null) {
										if (j != 0 && j < childQueryColumns.size() - 1) {
											sbf.append(cqc.getMosaic());
										}
										if (!cqc.getFiledExpre().equals(QueryExpreEnum.ISNOTNULL)
												&& !cqc.getFiledExpre().equals(QueryExpreEnum.ISNULL)
												&& !cqc.getFiledExpre().equals(QueryExpreEnum.IN)) {
											mf = new MessageFormat(cqc.getFiledName() + " "
													+ cqc.getFiledExpre().getExpre()
													+ " #'{'query.queryColumns[{0}].childQueryColumns[{1}].value'}' ");
										} else if (cqc.getFiledExpre().equals(QueryExpreEnum.IN)) {
											mf = new MessageFormat(cqc.getFiledName() + " "
													+ cqc.getFiledExpre().getExpre() + " (" + cqc.getValue() + ")");
										} else {
											mf = new MessageFormat(
													cqc.getFiledName() + " " + cqc.getFiledExpre().getExpre() + " ");
										}
										sbf.append(mf.format(new Object[] { i, j }));
									}
								}
								sbf.append(")");
								WHERE(sbf.toString());
							}
						}
					}
				}
			}
		}.toString();
	}

	// 计算总数
	public String getCount() {
		return new SQL() {
			{
				SELECT("count(${tableinfo.dbIdField})");
				FROM("${tableinfo.dbTableName}");
			}
		}.toString();
	}

	// 根据条件计算总条数p
	public String getCountByQuery(@Param("query")${tableinfo.tableName?cap_first}Query query) {
		return new SQL() {
			{
				SELECT("count(${tableinfo.dbIdField})");
				FROM("${tableinfo.dbTableName}");
				if (query!=null){
					List<QueryColumn> querys = query.getQueryColumns();
					if (querys != null && querys.size() > 0) {
						MessageFormat mf = null;
						for (int i = 0; i < querys.size(); i++) {
							QueryColumn qc = querys.get(i);
							if (i != 0) {
								if (qc.getMosaic().equals("AND")) {
									AND();
								} else {
									OR();
								}
							}
							if (qc.getFiledName() != null) {
								if (!qc.getFiledExpre().equals(QueryExpreEnum.ISNOTNULL)
										&& !qc.getFiledExpre().equals(QueryExpreEnum.ISNULL)
										&& !qc.getFiledExpre().equals(QueryExpreEnum.IN)) {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre()
											+ " #'{'query.queryColumns[{0}].value'}'");
								} else if (qc.getFiledExpre().equals(QueryExpreEnum.IN)) {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre() + " ("
											+ qc.getValue() + ")");
								} else {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre());
								}
								WHERE(mf.format(new Object[] { String.valueOf(i) }));
							}
							if (qc.getChildQueryColumns() != null && qc.getChildQueryColumns().size() > 0) {
								if (qc.getMosaic().equals("AND")) {
									AND();
								} else {
									OR();
								}
								List<QueryColumn> childQueryColumns = qc.getChildQueryColumns();
								StringBuffer sbf = new StringBuffer("(");
								for (int j = 0; j < childQueryColumns.size(); j++) {
									QueryColumn cqc = childQueryColumns.get(j);
									if (cqc.getFiledName() != null) {
										if (j != 0 && j < childQueryColumns.size() - 1) {
											sbf.append(cqc.getMosaic());
										}
										if (!cqc.getFiledExpre().equals(QueryExpreEnum.ISNOTNULL)
												&& !cqc.getFiledExpre().equals(QueryExpreEnum.ISNULL)
												&& !cqc.getFiledExpre().equals(QueryExpreEnum.IN)) {
											mf = new MessageFormat(cqc.getFiledName() + " " + cqc.getFiledExpre().getExpre()
													+ " #'{'query.queryColumns[{0}].childQueryColumns[{1}].value'}' ");
										} else if (cqc.getFiledExpre().equals(QueryExpreEnum.IN)) {
											mf = new MessageFormat(cqc.getFiledName() + " " + cqc.getFiledExpre().getExpre()
													+ " (" + cqc.getValue() + ")");
										} else {
											mf = new MessageFormat(
													cqc.getFiledName() + " " + cqc.getFiledExpre().getExpre() + " ");
										}
										sbf.append(mf.format(new Object[] { i, j }));
									}
								}
								sbf.append(")");
								WHERE(sbf.toString());
							}
						}
					}
				}
			}
		}.toString();
	}

	// 查询数据集合
	public String getList(@Param("query") ${tableinfo.tableName?cap_first}Query query) {
		StringBuffer hql = new StringBuffer(new SQL() {
			{
				if(query.getShowColumns()==null || query.getShowColumns().length==0) {
					SELECT("*");
				}else {
					SELECT(query.getShowColumns());
				}
				FROM("${tableinfo.dbTableName}");
				
				if (query!=null){
					List<QueryColumn> querys = query.getQueryColumns();
					if (querys != null && querys.size() > 0) {
						MessageFormat mf = null;
						for (int i = 0; i < querys.size(); i++) {
							QueryColumn qc = querys.get(i);
							if (i != 0) {
								if (qc.getMosaic().equals("AND")) {
									AND();
								} else {
									OR();
								}
							}
							if (qc.getFiledName() != null) {
								if (!qc.getFiledExpre().equals(QueryExpreEnum.ISNOTNULL)
										&& !qc.getFiledExpre().equals(QueryExpreEnum.ISNULL)
										&& !qc.getFiledExpre().equals(QueryExpreEnum.IN)) {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre()
											+ " #'{'query.queryColumns[{0}].value'}'");
								} else if (qc.getFiledExpre().equals(QueryExpreEnum.IN)) {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre() + " ("
											+ qc.getValue() + ")");
								} else {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre());
								}
								WHERE(mf.format(new Object[] { String.valueOf(i) }));
							}
							if (qc.getChildQueryColumns() != null && qc.getChildQueryColumns().size() > 0) {
								if (qc.getMosaic().equals("AND")) {
									AND();
								} else {
									OR();
								}
								List<QueryColumn> childQueryColumns = qc.getChildQueryColumns();
								StringBuffer sbf = new StringBuffer("(");
								for (int j = 0; j < childQueryColumns.size(); j++) {
									QueryColumn cqc = childQueryColumns.get(j);
									if (cqc.getFiledName() != null) {
										if (j != 0 && j < childQueryColumns.size() - 1) {
											sbf.append(cqc.getMosaic());
										}
										if (!cqc.getFiledExpre().equals(QueryExpreEnum.ISNOTNULL)
												&& !cqc.getFiledExpre().equals(QueryExpreEnum.ISNULL)
												&& !cqc.getFiledExpre().equals(QueryExpreEnum.IN)) {
											mf = new MessageFormat(cqc.getFiledName() + " " + cqc.getFiledExpre().getExpre()
													+ " #'{'query.queryColumns[{0}].childQueryColumns[{1}].value'}' ");
										} else if (cqc.getFiledExpre().equals(QueryExpreEnum.IN)) {
											mf = new MessageFormat(cqc.getFiledName() + " " + cqc.getFiledExpre().getExpre()
													+ " (" + cqc.getValue() + ")");
										} else {
											mf = new MessageFormat(
													cqc.getFiledName() + " " + cqc.getFiledExpre().getExpre() + " ");
										}
										sbf.append(mf.format(new Object[] { i, j }));
									}
								}
								sbf.append(")");
								WHERE(sbf.toString());
							}
						}
					}
					
					List<QueryOrder> orders=query.getOrders();
					if (orders!=null) {
						<#if config.type=='mysql'>
						//MySql的排序
						StringBuffer orderStr=new StringBuffer("");
						for (QueryOrder order:orders) {
							orderStr.append(order.getOrderField()).append(" ").append(order.getSort()).append(",");
						}
						if (orderStr.lastIndexOf(",")>-1) {
							orderStr.deleteCharAt(orderStr.lastIndexOf(","));
						}
						ORDER_BY(orderStr.toString());
						</#if>
					}
				}
			}
		}.toString());
		if (query != null) {
			QueryPageInfo pageInfo = query.getQueryPageInfo();
			if (pageInfo != null) {
				//mysql的版本
				<#if config.type=='mysql'>
				hql.append(" limit ").append(String.valueOf(pageInfo.getStartRow())).append(",")
						.append(String.valueOf(pageInfo.getRow()));
				</#if>
			}
		}
		return hql.toString();
	}


	public String get${tableinfo.tableName?cap_first}ByQuery(@Param("query")${tableinfo.tableName?cap_first}Query query) {
		StringBuffer hql = new StringBuffer(new SQL() {
			{
				SELECT("*");
				FROM("${tableinfo.dbTableName}");
				
				if (query!=null){
					List<QueryColumn> querys = query.getQueryColumns();
					if (querys != null && querys.size() > 0) {
						MessageFormat mf = null;
						for (int i = 0; i < querys.size(); i++) {
							QueryColumn qc = querys.get(i);
							if (i != 0) {
								if (qc.getMosaic().equals("AND")) {
									AND();
								} else {
									OR();
								}
							}
							if (qc.getFiledName() != null) {
								if (!qc.getFiledExpre().equals(QueryExpreEnum.ISNOTNULL)
										&& !qc.getFiledExpre().equals(QueryExpreEnum.ISNULL)
										&& !qc.getFiledExpre().equals(QueryExpreEnum.IN)) {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre()
											+ " #'{'query.queryColumns[{0}].value'}'");
								} else if (qc.getFiledExpre().equals(QueryExpreEnum.IN)) {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre() + " ("
											+ qc.getValue() + ")");
								} else {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre());
								}
								WHERE(mf.format(new Object[] { String.valueOf(i) }));
							}
							if (qc.getChildQueryColumns() != null && qc.getChildQueryColumns().size() > 0) {
								if (qc.getMosaic().equals("AND")) {
									AND();
								} else {
									OR();
								}
								List<QueryColumn> childQueryColumns = qc.getChildQueryColumns();
								StringBuffer sbf = new StringBuffer("(");
								for (int j = 0; j < childQueryColumns.size(); j++) {
									QueryColumn cqc = childQueryColumns.get(j);
									if (cqc.getFiledName() != null) {
										if (j != 0 && j < childQueryColumns.size() - 1) {
											sbf.append(cqc.getMosaic());
										}
										if (!cqc.getFiledExpre().equals(QueryExpreEnum.ISNOTNULL)
												&& !cqc.getFiledExpre().equals(QueryExpreEnum.ISNULL)
												&& !cqc.getFiledExpre().equals(QueryExpreEnum.IN)) {
											mf = new MessageFormat(cqc.getFiledName() + " " + cqc.getFiledExpre().getExpre()
													+ " #'{'query.queryColumns[{0}].childQueryColumns[{1}].value'}' ");
										} else if (cqc.getFiledExpre().equals(QueryExpreEnum.IN)) {
											mf = new MessageFormat(cqc.getFiledName() + " " + cqc.getFiledExpre().getExpre()
													+ " (" + cqc.getValue() + ")");
										} else {
											mf = new MessageFormat(
													cqc.getFiledName() + " " + cqc.getFiledExpre().getExpre() + " ");
										}
										sbf.append(mf.format(new Object[] { i, j }));
									}
								}
								sbf.append(")");
								WHERE(sbf.toString());
							}
						}
					}
					
					List<QueryOrder> orders=query.getOrders();
					if (orders!=null) {
						//MySql的排序
						StringBuffer orderStr=new StringBuffer("");
						for (QueryOrder order:orders) {
							orderStr.append(order.getOrderField()).append(" ").append(order.getSort()).append(",");
						}
						if (orderStr.lastIndexOf(",")>-1) {
							orderStr.deleteCharAt(orderStr.lastIndexOf(","));
						}
						ORDER_BY(orderStr.toString());
					}
				}
			}
		}.toString());
		hql.append(" limit 0,1");
		return hql.toString();
	}

	public String getUniqueResult(@Param("query")${tableinfo.tableName?cap_first}Query query,@Param("fieldName")String fieldName) {
		StringBuffer hql = new StringBuffer(new SQL() {
			{
				SELECT(fieldName);
				FROM("${tableinfo.dbTableName}");
				
				if (query!=null){
					List<QueryColumn> querys = query.getQueryColumns();
					if (querys != null && querys.size() > 0) {
						MessageFormat mf = null;
						for (int i = 0; i < querys.size(); i++) {
							QueryColumn qc = querys.get(i);
							if (i != 0) {
								if (qc.getMosaic().equals("AND")) {
									AND();
								} else {
									OR();
								}
							}
							if (qc.getFiledName() != null) {
								if (!qc.getFiledExpre().equals(QueryExpreEnum.ISNOTNULL)
										&& !qc.getFiledExpre().equals(QueryExpreEnum.ISNULL)
										&& !qc.getFiledExpre().equals(QueryExpreEnum.IN)) {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre()
											+ " #'{'query.queryColumns[{0}].value'}'");
								} else if (qc.getFiledExpre().equals(QueryExpreEnum.IN)) {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre() + " ("
											+ qc.getValue() + ")");
								} else {
									mf = new MessageFormat(qc.getFiledName() + " " + qc.getFiledExpre().getExpre());
								}
								WHERE(mf.format(new Object[] { String.valueOf(i) }));
							}
							if (qc.getChildQueryColumns() != null && qc.getChildQueryColumns().size() > 0) {
								if (qc.getMosaic().equals("AND")) {
									AND();
								} else {
									OR();
								}
								List<QueryColumn> childQueryColumns = qc.getChildQueryColumns();
								StringBuffer sbf = new StringBuffer("(");
								for (int j = 0; j < childQueryColumns.size(); j++) {
									QueryColumn cqc = childQueryColumns.get(j);
									if (cqc.getFiledName() != null) {
										if (j != 0 && j < childQueryColumns.size() - 1) {
											sbf.append(cqc.getMosaic());
										}
										if (!cqc.getFiledExpre().equals(QueryExpreEnum.ISNOTNULL)
												&& !cqc.getFiledExpre().equals(QueryExpreEnum.ISNULL)
												&& !cqc.getFiledExpre().equals(QueryExpreEnum.IN)) {
											mf = new MessageFormat(cqc.getFiledName() + " " + cqc.getFiledExpre().getExpre()
													+ " #'{'query.queryColumns[{0}].childQueryColumns[{1}].value'}' ");
										} else if (cqc.getFiledExpre().equals(QueryExpreEnum.IN)) {
											mf = new MessageFormat(cqc.getFiledName() + " " + cqc.getFiledExpre().getExpre()
													+ " (" + cqc.getValue() + ")");
										} else {
											mf = new MessageFormat(
													cqc.getFiledName() + " " + cqc.getFiledExpre().getExpre() + " ");
										}
										sbf.append(mf.format(new Object[] { i, j }));
									}
								}
								sbf.append(")");
								WHERE(sbf.toString());
							}
						}
					}
					
					List<QueryOrder> orders=query.getOrders();
					if (orders!=null) {
						//MySql的排序
						StringBuffer orderStr=new StringBuffer("");
						for (QueryOrder order:orders) {
							orderStr.append(order.getOrderField()).append(" ").append(order.getSort()).append(",");
						}
						if (orderStr.lastIndexOf(",")>-1) {
							orderStr.deleteCharAt(orderStr.lastIndexOf(","));
						}
						ORDER_BY(orderStr.toString());
					}
				}
			}
		}.toString());
		hql.append(" limit 0,1");
		return hql.toString();
	}
}
